package array

// https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnhhkv/
// 矩阵旋转90度
// 算法: 遍历
func Rotate(matrix [][]int) {
	arrLen := len(matrix)
	// 从外圈到里层，不断交换
	for y := 0; y < arrLen/2; y++ {
		for x := y; x < arrLen-y-1; x++ {
			// 正方形，交换4次即可
			currentNum := matrix[x][y]
			nextX, nextY := x, y
			for index := 0; index < 4; index++ {
				nextX, nextY = getNextPos(nextX, nextY, arrLen)
				currentNum, matrix[nextX][nextY] = matrix[nextX][nextY], currentNum
			}
		}
	}
}

// 公共方法: 获取下一个位置
func getNextPos(x, y, len int) (nextX, nextY int) {
	nextX = y
	nextY = len - x - 1
	// len = 4: 0, 1 -> 1, 3
	return
}
